﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using shradhabookstores;
using PagedList;

namespace shradhabookstores.Controllers
{
    public class OrderDetailController : Controller
    {
        private shradhabookstoresEntities5 db = new shradhabookstoresEntities5();

        // GET: /OrderDetail/
        public ActionResult Index(string sortOrder, string currentFilter, string SearchString, int? page)
        {
            if (Session["UserName"] == null)
            {
                return RedirectToAction("Login", "AccountAdmin");
            }
            var orderdetails = db.OrderDetails.Include(o => o.Product).Include(o => o.Order);
           
            ViewBag.CurrentSort = sortOrder;
            ViewBag.ProductIDSortParm = String.IsNullOrEmpty(sortOrder) ? "ProductID_desc" : "";
          //   ViewBag.LevelSortParm = sortOrder == "Level_" ? "Level_desc" : "Level_";
            if (SearchString != null)
            {
                page = 1;
            }
            else
            {
                SearchString = currentFilter;
            }

            ViewBag.CurrentFilter = SearchString;

            var user = from u in db.OrderDetails
                       select u;
            if (!String.IsNullOrEmpty(SearchString))
            {
                user = user.Where(u => u.ProductID.ToUpper().Contains(SearchString.ToUpper())
                  //      || u.Level_.ToUpper().Contains(SearchString.ToUpper())
                    );
            }
            switch (sortOrder)
            {
                case "ProductID_desc":
                    user = user.OrderByDescending(u => u.ProductID);
                    break;
                //case "Level_":
                //    user = user.OrderByDescending(u => u.Level_);
                //    break;
                default:
                    user = user.OrderBy(u => u.ProductID);
                    break;

            }
            int pageSize = 10;
            int pageNumber = (page ?? 1);
            return View(user.ToPagedList(pageNumber, pageSize));
        }
        

        // GET: /OrderDetail/Details/5
        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            OrderDetail orderdetail = db.OrderDetails.Find(id);
            if (orderdetail == null)
            {
                return HttpNotFound();
            }
            return View(orderdetail);
        }

        // GET: /OrderDetail/Create
        public ActionResult Create()
        {
            ViewBag.ProductID = new SelectList(db.Products, "ID", "Name");
            ViewBag.OrdersID = new SelectList(db.Orders, "ID", "note");
            return View();
        }

        // POST: /OrderDetail/Create
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include="OrdersID,ID,ProductID,Quantity,Sales,Money")] OrderDetail orderdetail)
        {
            if (ModelState.IsValid)
            {
                db.OrderDetails.Add(orderdetail);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.ProductID = new SelectList(db.Products, "ID", "Name", orderdetail.ProductID);
            ViewBag.OrdersID = new SelectList(db.Orders, "ID", "note", orderdetail.OrdersID);
            return View(orderdetail);
        }

        // GET: /OrderDetail/Edit/5
        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            OrderDetail orderdetail = db.OrderDetails.Find(id);
            if (orderdetail == null)
            {
                return HttpNotFound();
            }
            ViewBag.ProductID = new SelectList(db.Products, "ID", "Name", orderdetail.ProductID);
            ViewBag.OrdersID = new SelectList(db.Orders, "ID", "note", orderdetail.OrdersID);
            return View(orderdetail);
        }

        // POST: /OrderDetail/Edit/5
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include="OrdersID,ID,ProductID,Quantity,Sales,Money")] OrderDetail orderdetail)
        {
            if (ModelState.IsValid)
            {
                db.Entry(orderdetail).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.ProductID = new SelectList(db.Products, "ID", "Name", orderdetail.ProductID);
            ViewBag.OrdersID = new SelectList(db.Orders, "ID", "note", orderdetail.OrdersID);
            return View(orderdetail);
        }

        // GET: /OrderDetail/Delete/5
        public ActionResult Delete(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            OrderDetail orderdetail = db.OrderDetails.Find(id);
            if (orderdetail == null)
            {
                return HttpNotFound();
            }
            return View(orderdetail);
        }

        // POST: /OrderDetail/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            OrderDetail orderdetail = db.OrderDetails.Find(id);
            db.OrderDetails.Remove(orderdetail);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}
